﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Web.Services.Description;
using System.Web.Services.Discovery;
using System.Net;

namespace ParseWSDL
{
    public partial class ParseWSDLWindow : Form
    {
        public ParseWSDLWindow()
        {
            InitializeComponent();
            txtURL.Text = @"http://moss2010-hn/_vti_bin/CMISSoapwsdl.aspx";
        }

        private DiscoveryClientProtocol CreateDiscoveryClient()
        {
            string userName = "administrator";
            string password = "K00fax";
            string domain = "moss2010-hn";
            DiscoveryClientProtocol protocol = new DiscoveryClientProtocol();
            protocol.AllowAutoRedirect = true;
            if (userName != null)
            {
                protocol.Credentials = new NetworkCredential(userName, password, domain);
            }
            else
            {
                protocol.Credentials = CredentialCache.DefaultCredentials;
            }
            return protocol;
        }

        private void btnGetFile_Click(object sender, EventArgs e)
        {
            //http://cmis.demo.nuxeo.org/nuxeo/webservices/cmis/RepositoryService?wsdl
            //http://moss2010-hn/_vti_bin/CMISSoapwsdl.aspx

            string userName = "administrator";
            string password = "K00fax";
            string domain = "moss2010-hn";
            //userName = null;

            //WebRequest webRequest = WebRequest.Create(new Uri(txtURL.Text));
            HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(txtURL.Text);
            if (userName != null)
            {
                webRequest.Credentials = new NetworkCredential(userName, password, domain);
            }
            else
            {
                webRequest.Credentials = CredentialCache.DefaultCredentials;
            }
            webRequest.PreAuthenticate = true;
            webRequest.AllowAutoRedirect = true;
            System.IO.Stream requestStream = webRequest.GetResponse().GetResponseStream();
            XmlTextReader reader = new XmlTextReader(requestStream);

            ServiceDescription wsdl = ServiceDescription.Read(reader);

            foreach (Service service in wsdl.Services)
            {
                foreach (Port port in service.Ports)
                {
                    foreach (ServiceDescriptionFormatExtension extension in port.Extensions)
                    {
                        SoapAddressBinding address = (SoapAddressBinding)extension;
                        MessageBox.Show(address.Location);
                    }
                }
            }
        }

        private void btnGetFile2_Click(object sender, EventArgs e)
        {
            DiscoveryClientProtocol client = CreateDiscoveryClient();
            DiscoveryDocument document = client.DiscoverAny(txtURL.Text);
            client.ResolveAll();

        }

        private void btnGetFile3_Click(object sender, EventArgs e)
        {
            StringBuilder output = new StringBuilder();
            XmlWriterSettings ws = new XmlWriterSettings();

            WebRequest webRequest = WebRequest.Create(new Uri(txtURL.Text));
            System.IO.Stream requestStream = webRequest.GetResponse().GetResponseStream();
            XmlTextReader reader = new XmlTextReader(requestStream);
            //while (reader.Read())
            //{
            //    if (reader.Value.Contains("e"))
            //    {
            //        MessageBox.Show(reader.Value);
            //    }
            //}

            using (XmlWriter writer = XmlWriter.Create(output, ws))
            {

                // Parse the file and display each of the nodes.
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            MessageBox.Show(reader.Name);
                            break;
                        case XmlNodeType.Text:
                            writer.WriteString(reader.Value);
                            break;
                        case XmlNodeType.XmlDeclaration:
                        case XmlNodeType.ProcessingInstruction:
                            writer.WriteProcessingInstruction(reader.Name, reader.Value);
                            break;
                        case XmlNodeType.Comment:
                            writer.WriteComment(reader.Value);
                            break;
                        case XmlNodeType.EndElement:
                            writer.WriteFullEndElement();
                            break;
                    }
                }

            }
        }
    }
}
